package com.liang.hellocompose.net

import android.util.Log
import com.liang.moshijson.NullJsonFactory
import com.squareup.moshi.Moshi
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit


/**
 * @author suzl
 * @createTime 2021/8/3
 * @Description
 */
object ApiService {

    private val retrofit: Retrofit by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
        val clientBuild =
            OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .callTimeout(30, TimeUnit.SECONDS)
                .dispatcher(Dispatcher().apply {
                    maxRequestsPerHost = 10
                })
                .addInterceptor(CustomInterceptor())

        clientBuild.addInterceptor(HttpLoggingInterceptor { message ->
            Log.e("OKHttp-----", message)
        }.apply {
            level = HttpLoggingInterceptor.Level.BODY
        })

        val moShi = Moshi.Builder()
            .add(NullJsonFactory.STANDARD)
            .build()

        Retrofit.Builder()
            .baseUrl("https://www.wanandroid.com")
            .addConverterFactory(MoshiConverterFactory.create(moShi))
            .client(clientBuild.build())
            .build()
    }

    val wanAndroidApi: WanAndroidApi by lazy {
        retrofit.create(WanAndroidApi::class.java)
    }

}